class Order < ActiveRecord::Base  
  has_many                :items
  has_many                :order_extras
  has_many                :order_options, :through => :order_extras
  has_many                :photos,        :through => :items
  has_many                :videos,        :through => :items
  belongs_to              :user
  belongs_to              :shipping_method
  belongs_to              :invoice
  validates_presence_of   :user
  
  before_save             :assign_default_shipping_method_if_none_is_set
  after_destroy           :destroy_dependents
  before_save             :update_order_items
  before_save             :assign_order_options
  
  def includes_option?(order_option)
    self.order_extras.detect{|order_extra| order_extra.order_option == order_option} ? true : false
  end
  
  def grand_total
    grand_total = 0
    self.order_extras.each {|order_extra| grand_total = grand_total + (order_extra.order_option.cost * (order_extra.order_option.quantifiable? ? order_extra.quantity : 1))} unless self.order_extras.empty?
    self.items.each do |item|
      (grand_total = grand_total + item.options_cost + (item.cost * item.quantity)) if item.photo
      (grand_total = grand_total + (item.options_cost * item.quantity)) if item.video
    end
    grand_total += self.tax
    (grand_total += self.shipping_method.cost) if self.shipping_method
    grand_total
  end
  
  def tax
    tax = 0
    self.order_extras.each {|order_extra| tax = tax + (order_extra.order_option.cost * (order_extra.order_option.quantifiable? ? order_extra.quantity : 1))} unless self.order_extras.empty?
    self.items.each do |item|
      (tax = tax + item.options_cost + (item.cost * item.quantity)) if item.photo
      (tax = tax + (item.options_cost * item.quantity)) if item.video
    end
    tax *= 0.06
  end
  
  def order_options=(order_options = [])
    @order_options = order_options
  end
  
  def order_items=(order_items = [])
    @order_items = order_items
  end
  
protected
  def assign_default_shipping_method_if_none_is_set
    (self.shipping_method = ShippingMethod.find(:all, :order => 'cost asc').first) unless self.shipping_method
  end
  
  def update_order_items
    return if @order_items.nil?
    @order_items.each do |item|
      if item[1]["photo_id"]
        if item[1]["remove"] || (item[1]["quantity"].first == "0")
          Item.find(item.first.to_f).destroy
        else
          existing_item = Item.find(item.first.to_f)
          (existing_item.quantity = item[1]["quantity"].first.to_i) if item[1]["quantity"]
          (existing_item.photo_size_id = item[1]["photo_size_id"].first.to_i) if item[1]["photo_size_id"]
          (existing_item.price_list_id = item[1]["price_list_id"].first.to_i) if item[1]["price_list_id"]
          existing_item.itemizations.each {|itemization| itemization.destroy} if existing_item.itemizations
          item[1]["item_options_ids"].each {|item_option_id| Itemization.new(:item => existing_item, :item_option_id => item_option_id).save!} if item[1]["item_options_ids"]
          existing_item.save!
        end
      elsif item[1]["video_id"]
        if item[1]["remove"] || (item[1]["quantity"].first == "0")
          Item.find(item.first.to_f).destroy
        else
          existing_item = Item.find(item.first.to_f)
          (existing_item.quantity = item[1]["quantity"].first.to_i) if item[1]["quantity"]
          existing_item.itemizations.each {|itemization| itemization.destroy}
          item[1]["item_options_ids"].each {|item_option_id| Itemization.new(:item => existing_item, :item_option_id => item_option_id).save!} if item[1]["item_options_ids"]
          existing_item.save!
        end
      end
    end
  end
  
  def assign_order_options
    return if @order_options.nil?
    (self.order_extras.each {|order_extra| order_extra.destroy}) if self.order_extras
    @order_options.each do |order_option|
      if OrderOption.find(order_option.first).quantifiable? and order_option[1]["quantity"].first.to_i > 0
        OrderExtra.create!(:order_id => self.id, :order_option_id => order_option.first, :quantity => order_option[1]["quantity"].first.to_i)
      elsif !OrderOption.find(order_option.first).quantifiable?
        OrderExtra.create!(:order_id => self.id, :order_option_id => order_option.first)
      end
    end
  end
  
  def destroy_dependents
    self.order_extras.each {|order_extra| order_extra.destroy} unless self.order_extras.empty?
    self.items.each {|item| item.destroy} unless self.items.empty?
  end
end
